package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.mapper.common;

import java.util.HashSet;
import java.util.Set;

import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLEntity;

import com.google.common.base.Optional;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.menu.Menu;
import com.gwtext.client.widgets.menu.MenuItem;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.stanford.bmir.protege.web.client.dispatch.DispatchServiceManager;
import edu.stanford.bmir.protege.web.client.dispatch.actions.AbstractCreateEntityInHierarchyAction;
import edu.stanford.bmir.protege.web.client.dispatch.actions.AbstractCreateEntityInHierarchyResult;
import edu.stanford.bmir.protege.web.client.dispatch.actions.CreateAnnotationPropertiesAction;
import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.data.EntityData;
import edu.stanford.bmir.protege.web.client.ui.library.msgbox.MessageBox;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.treepanel.StkosClassTreePanel;

/**
 * Stkos Mapper Class Tree Panel
 * 
 * @author LWS
 * 
 */
public class StkosMapperClassTreePanel extends StkosClassTreePanel {

	private Project accordingProject;

	public StkosMapperClassTreePanel(Project project) {
		this(project, null);
	}

	public StkosMapperClassTreePanel(Project project, Project accordingProject) {
		super(project);
		this.accordingProject = accordingProject;
	}

	@Override
	protected TreeNode createTreeNode(EntityData entityData) {
		TreeNode node = super.createTreeNode(entityData);
		if (node == null) {
			return node;
		}

		if (accordingProject != null) {
			addAnnotationMappingNoNodeMark(node, entityData, accordingProject
					.getProjectId().getId());
		}

		return node;
	}

	@Override
	protected void onContextMenu(final TreeNode node, final EventObject e) {
		super.onContextMenu(node, e);

		Menu contextMenu = new Menu();

		if (node != null && getProject() != null) {
			MenuItem menuModifyMapperNumber = new StkosMapperModifyMapperNumberMenuItem(
					this, getProject(), accordingProject, node);
			contextMenu.addItem(menuModifyMapperNumber);
			
			MenuItem menuDeleteMapperNumber = new StkosMapperDeleteMapperNumberMenuItem(
					this, getProject(), accordingProject, node);
			contextMenu.addItem(menuDeleteMapperNumber);
		}

		contextMenu.showAt(e.getXY()[0] + 5, e.getXY()[1] + 5);
	}

	private void createAnnotation() {

		if (getProject() == null || getProject().getProjectId() == null) {
			return;
		}

		Set<String> newAnnotationTexts = new HashSet<String>();
		newAnnotationTexts
				.add(StkosMapperConstant.STKOSANNOTATIONPROPERTYMAPPERLABEL);

		Optional<OWLAnnotationProperty> parent = Optional
				.<OWLAnnotationProperty> absent();

		CreateAnnotationPropertiesAction action = new CreateAnnotationPropertiesAction(
				getProject().getProjectId(), newAnnotationTexts, parent);

		createSubProperties(action);
	}

	private <R extends AbstractCreateEntityInHierarchyResult<E>, E extends OWLEntity> void createSubProperties(
			AbstractCreateEntityInHierarchyAction<R, E> action) {
		DispatchServiceManager.get().execute(action, new AsyncCallback<R>() {
			@Override
			public void onFailure(Throwable caught) {
				GWT.log("-------------------->>> There was a problem creating the properties",
						caught);
			}

			@Override
			public void onSuccess(R result) {
				System.out.println("-----------------------------");
				System.out.println(result.getEntities());
				System.out.println("-----------------------------");
			}
		});
	}

	public void setAccordingProject(Project accordingProject) {
		this.accordingProject = accordingProject;
		onRefresh();
	}
}
